// pages/address/list/list.js
import { getAddressListApi, delAddressApi } from '../../../../api/address';

const app = getApp();

Page({
  data: {
    addressList: []
  },
  onShow() {
    this.getAllAddress();
  },
  async getAllAddress() {
    const addressList = await getAddressListApi();
    this.setData({
      addressList
    })
  },
  delAddress(e) {
    wx.showModal({
      title: '删除',
      content: '您确认要删除当前地址吗?',
      complete: async (res) => {
        if (res.confirm) {
          await delAddressApi(e.currentTarget.dataset.id);
          this.getAllAddress();
        }
      }
    })
  },
  goOrderDetail(e) {
    const { item } = e.currentTarget.dataset;
    app.globalData.selectedAddress = item;
    wx.navigateBack();
  },
  goEdit(e) {
    wx.navigateTo({
      url: `../add/add?id=${ e.currentTarget.dataset.id }`,
    })
  }
})